//
//  OauthViewController.swift
//  微博Swift
//
//  Created by apple on 15/9/23.
//  Copyright © 2015年 cc. All rights reserved.
//

import UIKit

class OauthViewController: UIViewController {
    // 网页界面
    lazy var webView = UIWebView()
    
    override func loadView() {
        view = webView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置导航栏信息
        setupNavInfo()

        // 加载授权页面
        webView.loadRequest(NSURLRequest(URL: NetWorkingTools.sharedTools.oauthURL))
    }
    
    func setupNavInfo() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: UIBarButtonItemStyle.Plain, target: self, action: "autoFill")
        
        title = "微博授权登录"
    }
    
    // MARK: -监听方法
    @objc private func close() {
        print("关闭")
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func autoFill() {
        print("自动填充")
        
        let js = "document.getElementById('userId').value = '261983659@qq.com';" + "document.getElementById('passwd').value = 'cs234551';"
        
        webView.stringByEvaluatingJavaScriptFromString(js)
    }
}
